package com.puboot.module.blog.controller;

import com.puboot.common.util.*;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.*;
import com.puboot.module.blog.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/magnesiumsmelting/api")
@AllArgsConstructor
public class MagnesiumSmeltingController {

    private final HttpServletRequest httpServletRequest;
    private final MagnesiumSmeltingService magnesiumSmeltingService;
    private final MagnesiumSmeltingDetailService magnesiumSmeltingDetailService;

    @PostMapping("/result")
    @Transactional
    public ResponseVo result(MagnesiumSmelting magnesiumSmelting) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> ncvs = new ArrayList<>();
            if (StringUtils.isNotBlank(magnesiumSmelting.getNcv())) {
                ncvs = Arrays.asList(magnesiumSmelting.getNcv().split(","));
            }
            List<String> fcs = new ArrayList<>();
            if (StringUtils.isNotBlank(magnesiumSmelting.getFc())) {
                fcs = Arrays.asList(magnesiumSmelting.getFc().split(","));
            }
            List<String> ccs = new ArrayList<>();
            if (StringUtils.isNotBlank(magnesiumSmelting.getCc())) {
                ccs = Arrays.asList(magnesiumSmelting.getCc().split(","));
            }
            List<String> ofs = new ArrayList<>();
            if (StringUtils.isNotBlank(magnesiumSmelting.getOf())) {
                ofs = Arrays.asList(magnesiumSmelting.getOf().split(","));
            }
            Double eBurn = MagnesiumSmeltingUtil.getEBurn(ncvs, fcs, ccs, ofs);
            Double eRawmaterial = MagnesiumSmeltingUtil.getERawmaterial(String.valueOf(magnesiumSmelting.getS()));
            Double eProcess = MagnesiumSmeltingUtil.getEProcess(String.valueOf(magnesiumSmelting.getD()));
            Double ePurchasedElectricity = MagnesiumSmeltingUtil.getEPurchasedElectricity(String.valueOf(magnesiumSmelting.getAdElectricOutsourcing()), String.valueOf(magnesiumSmelting.getEfElectric()));
            Double ePurchaseHeat = MagnesiumSmeltingUtil.getEPurchaseHeatOreOutputHeat(String.valueOf(magnesiumSmelting.getAdHeatOutsourcing()));
            Double eOutputHeat = MagnesiumSmeltingUtil.getEPurchaseHeatOreOutputHeat(String.valueOf(magnesiumSmelting.getAdHeatOutput()));
            Double eOutputElectricity = MagnesiumSmeltingUtil.getEOutputElectricity(String.valueOf(magnesiumSmelting.getAdElectricOutput()), String.valueOf(magnesiumSmelting.getEfElectricOut()));

            Double result = MagnesiumSmeltingUtil.getE(String.valueOf(eBurn), String.valueOf(eRawmaterial), String.valueOf(eProcess),
                    String.valueOf(ePurchasedElectricity), String.valueOf(ePurchaseHeat), String.valueOf(eOutputElectricity),
                    String.valueOf(eOutputHeat));
            magnesiumSmelting.setCustomerId(customer.getId());
            magnesiumSmelting.setCreateTime(new Date());
            magnesiumSmelting.setResult(GasDCUtil.getThousandth(result));
            magnesiumSmelting.setEBurn(GasDCUtil.round(eBurn, 2));
            magnesiumSmelting.setERawmaterial(GasDCUtil.round(eRawmaterial, 2));
            magnesiumSmelting.setEProcess(GasDCUtil.round(eProcess, 2));
            magnesiumSmelting.setEPurchasedElectricity(GasDCUtil.round(ePurchasedElectricity, 2));
            magnesiumSmelting.setEPurchaseHeat(GasDCUtil.round(ePurchaseHeat, 2));
            magnesiumSmelting.setEOutputHeat(GasDCUtil.round(eOutputHeat, 2));
            magnesiumSmelting.setEOutputElectricity(GasDCUtil.round(eOutputElectricity, 2));
            magnesiumSmelting.setState(Constants.STATE_INVAILD);
            magnesiumSmeltingService.save(magnesiumSmelting);
            for (int i = 0; i < ncvs.size(); i++) {
                MagnesiumSmeltingDetail detail = new MagnesiumSmeltingDetail();
                detail.setNcv(Double.valueOf(ncvs.get(i)));
                detail.setFc(Double.valueOf(fcs.get(i)));
                detail.setCc(Double.valueOf(ccs.get(i)));
                detail.setOf(Double.valueOf(ofs.get(i)));
                magnesiumSmeltingDetailService.save(detail);
            }
            return ResultUtil.success("计算成功", magnesiumSmelting);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }


}
